/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;

/**
 *
 * @author Manish
 */
public class EditResume extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String resume_title = "", res_keyskill = "", resumetxt = "";
        String desired_ind = "", desired_fa = "", desired_salary = "", des_keyskill = "";
        String radio = "";
        String exp_current_co = "", current_ind = "", exp_current_jobrole = "", current_salary = "", tot_exp_years = "";
        String edudtl = "";
        String course10 = "", uni10 = "",stream10 = "", year10 = "", mark10 = "", course12 = "", uni12 = "", stream12 = "", year12 = "", mark12 = "", courseug = "", uniug = "", streamug = "", yearug = "", markug = "", coursepg = "", unipg = "", streampg = "", yearpg = "", markpg = "";
        String vercode = "";
        String sql = "";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd=request.getRequestDispatcher("editresume.jsp");

        try {
            con = Dbconn.getConn();
            st = con.createStatement();


            //updating resume table
            resume_title = request.getParameter("resume_title");
            res_keyskill = request.getParameter("res_keyskill");
            resumetxt = request.getParameter("restxt");
            out.println("Resume Text="+resumetxt);

            sql = "update resume set title='" + resume_title + "', keyskill='" + res_keyskill + "',textresume='" + resumetxt + "', date=now() where username='" + username + "'";
            if (st.executeUpdate(sql) <= 0) {
                sql = "insert into resume(title,keyskill,textresume,username,date) values('" + resume_title + "','" + res_keyskill + "','" + resumetxt + "','" + username + "',now())";
                st.executeUpdate(sql);
            }

            //update Desired job Details
            desired_ind = request.getParameter("desired_ind");
            desired_fa = request.getParameter("desired_fa");
            desired_salary = request.getParameter("desired_salary");
            des_keyskill = request.getParameter("des_keyskill");
            sql = "update desiredjob set industry='" + desired_ind + "', fun_area='" + desired_fa + "',keyskill='" + des_keyskill + "',salary='" + desired_salary + "'  where username='" + username + "'";
            if (st.executeUpdate(sql) <= 0) {
                sql = "insert into desiredjob(username,industry,fun_area,keyskill,salary) values('" + username + "','" + desired_ind + "','" + desired_fa + "','" + des_keyskill + "','" + desired_salary + "')";
                st.executeUpdate(sql);
            }


            //update Professional details
            radio = request.getParameter("radio");
            if (radio.equalsIgnoreCase("Fresher")) {
                sql = "delete from exprience where username='" + username + "'";
                st.executeUpdate(sql);
            } else {
                out.println("updating exp");
                //exp__co=asdf&_ind=Biotech/Pharma&exp__jobrole=sdf&current_salary=15000.00&tot_exp_yea
                exp_current_co = request.getParameter("exp__co");
                current_ind = request.getParameter("_ind");
                exp_current_jobrole = request.getParameter("exp__jobrole");
                current_salary = request.getParameter("current_salary");
                tot_exp_years = request.getParameter("tot_exp_years");
                sql = "update exprience set employer='" + exp_current_co + "', industry='" + current_ind + "',role='" + exp_current_jobrole + "',salary='" + current_salary + "',exp='" + tot_exp_years + "'  where username='" + username + "'";
                if (st.executeUpdate(sql) <= 0) {
                    out.println("inserting exp");
                    sql = "insert into exprience(username,employer,industry,role,salary,exp) values('" + username + "','" + exp_current_co + "','" + current_ind + "','" + exp_current_jobrole + "','" + current_salary + "','" + tot_exp_years + "')";
                    st.executeUpdate(sql);
                }
            }

            //Updating Education Details
            if (request.getParameter("course10") != null) {
                course10 = request.getParameter("course10");
            }
            if (request.getParameter("uni10") != null) {
                uni10 = request.getParameter("uni10");
            }
            if (request.getParameter("stream10") != null) {
                stream10 = request.getParameter("stream10");
            }
            if (request.getParameter("year10") != null) {
                year10 = request.getParameter("year10");
            }
            if (request.getParameter("mark10") != null) {
                mark10 = request.getParameter("mark10");
            }
            if (request.getParameter("course12") != null) {
                course12 = request.getParameter("course12");
            }
            if (request.getParameter("uni12") != null) {
                uni12 = request.getParameter("uni12");
            }
            if (request.getParameter("stream12") != null) {
                stream12 = request.getParameter("stream12");
            }
            if (request.getParameter("year12") != null) {
                year12 = request.getParameter("year12");
            }
            if (request.getParameter("mark12") != null) {
                mark12 = request.getParameter("mark12");
            }
            if (request.getParameter("courseug") != null) {
                courseug = request.getParameter("courseug");
            }
            if (request.getParameter("uniug") != null) {
                uniug = request.getParameter("uniug");
            }
            if (request.getParameter("streamug") != null) {
                streamug = request.getParameter("streamug");
            }
            if (request.getParameter("yearug") != null) {
                yearug = request.getParameter("yearug");
            }
            if (request.getParameter("markug") != null) {
                markug = request.getParameter("markug");
            }
            if (request.getParameter("coursepg") != null) {
                coursepg = request.getParameter("coursepg");
            }
            if (request.getParameter("unipg") != null) {
                unipg = request.getParameter("unipg");
            }
            if (request.getParameter("streampg") != null) {
                streampg = request.getParameter("streampg");
            }
            if (request.getParameter("yearpg") != null) {
                yearpg = request.getParameter("yearpg");
            }
            if (request.getParameter("markpg") != null) {
                markpg = request.getParameter("markpg");
            }



            edudtl = request.getParameter("edudtl");
            int i = Integer.parseInt(edudtl);

            switch (i) {
                //update pg
                case 4:
                    sql = "update education set course='"+coursepg+"', univ='"+unipg+"', stream='"+streampg+"', year='"+yearpg+"', marks='"+markpg+"' where username='"+username+"' and type='4'";
                    if(st.executeUpdate(sql)<=0){
                        sql="insert into education(username,course,univ,stream,year,marks,type) values('"+username+"','"+coursepg+"','"+unipg+"','"+streampg+"','"+yearpg+"','"+markpg+"','4')";
                        out.println(sql);
                        st.executeUpdate(sql);
                    }
                case 3://update ug
                     sql = "update education set course='"+courseug+"', univ='"+uniug+"', stream='"+streamug+"', year='"+yearug+"', marks='"+markug+"' where username='"+username+"' and type='3'";
                    if(st.executeUpdate(sql)<=0){
                        sql="insert into education(username,course,univ,stream,year,marks,type) values('"+username+"','"+courseug+"','"+uniug+"','"+streamug+"','"+yearug+"','"+markug+"','3')";
                        st.executeUpdate(sql);
                    }
                case 2://update 12
                     sql = "update education set course='"+course12+"', univ='"+uni12+"', stream='"+stream12+"', year='"+year12+"', marks='"+mark12+"' where username='"+username+"' and type='2'";
                    if(st.executeUpdate(sql)<=0){
                        sql="insert into education(username,course,univ,stream,year,marks,type) values('"+username+"','"+course12+"','"+uni12+"','"+stream12+"','"+year12+"','"+mark12+"','2')";
                        st.executeUpdate(sql);
                    }
                case 1://update 10
                     sql = "update education set course='"+course10+"', univ='"+uni10+"', stream='"+stream10+"', year='"+year10+"', marks='"+mark10+"' where username='"+username+"' and type='1'";
                     out.println(sql);
                    if(st.executeUpdate(sql)<=0){
                        sql="insert into education(username,course,univ,stream,year,marks,type) values('"+username+"','"+course10+"','"+uni10+"','"+stream10+"','"+year10+"','"+mark10+"','1')";
                        st.executeUpdate(sql);
                    }
                     break;
                default:
                    sql = "";
            }
            switch (i) {
                case 1:
                    sql = "delete from education where username='" + username + "' and type='2'";
                    st.executeUpdate(sql);
                case 2:
                    sql = "delete from education where username='" + username + "' and type='3'";
                    st.executeUpdate(sql);
                case 3:
                    sql = "delete from education where username='" + username + "' and type='4'";
                    st.executeUpdate(sql);


            }

            rd.forward(request, response);
        } catch (Exception e) {
            out.println("Exception::" + e);

        } finally {
            out.close();
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
